package com.atguigu.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.exception.PhoneExisException;
import com.atguigu.gulimall.member.exception.RegistException;
import com.atguigu.gulimall.member.exception.UserNameExisException;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberUserRegistVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author mayp
 * @email 1582240996@qq.com
 * @date 2020-06-06 09:02:01
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 被远程调用的注册功能
     * @param vo
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberUserRegistVo vo){
        try{
            memberService.regist(vo);
        }catch (PhoneExisException e){
            return R.error(BizCodeEnume.PHONE_EXIST_EXPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXPTION.getMsg());
        }catch (UserNameExisException e){
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    /**
     * 账户密码登录功能（查询数据库，根据姓名或者电话查询数据库，对密码匹配，如果登录成功，返回Member实体（用户实体））
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity = memberService.login(vo);
        if (entity != null){
            return R.ok().setData(entity);
        }
        return R.error(BizCodeEnume.USERNAME_PASSWORD_ERROR.getCode(),BizCodeEnume.USERNAME_PASSWORD_ERROR.getMsg());
    }

    /**
     * 社交登录功能
     */
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser vo){
        MemberEntity entity = memberService.login(vo);
        if (entity != null){
            return R.ok().setData(entity);
        }
        return R.error(BizCodeEnume.USERNAME_PASSWORD_ERROR.getCode(),BizCodeEnume.USERNAME_PASSWORD_ERROR.getMsg());
    }
}
